package fleditor.view
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.NativeWindowDisplayState;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import fleditor.common.DialogBoxManager;
	import fleditor.controller.FlEditorController;
	import fleditor.events.FlEditorEvent;
	import fleditor.model.FlEditorModel;
	
	import ui.top.menuBgUI;
	import ui.top.topBarBgRightUI;
	import ui.top.topBarBgUI;
	import ui.top.topBarUI;

	/**
	 * 顶部操作界面 
	 * @author Devin
	 * 
	 */	
	public class TopBarView extends topBarUI
	{
		private var _controller:FlEditorController;
		private var _model:FlEditorModel;
		private var _topBarBgUI:topBarBgUI;
		private var _bmTopBarBg:Bitmap;
		private var _bmTopBarRight:Bitmap;
		private var _createMapView:CreateMapView;
		private var _menuBg:Sprite;//二级菜单条背景
		private var _menuType:int=1;//当前一级菜单类型：1=物件编辑；2=地图编辑；3=地图预览
		private var _menuAssetType:int=1;//当前物件编辑二级菜单类型：1=单项编辑；2=组合编辑；3=区域编辑
		
		public function TopBarView(controller:FlEditorController, model:FlEditorModel)
		{
			_controller=controller;
			_model=model;
			setView();
			setEvent();
		}
		
		/**
		 *设置视图 
		 */		
		private function setView():void
		{
			//物件编辑、地图编辑、地图测试操作按钮设置为button模式，以支持手形鼠标显示
			btnComponentEdit.buttonMode=btnMapEdit.buttonMode=btnMapTest.buttonMode=true;
			bgBar.doubleClickEnabled=true;
			
			resetButton();//复位操作按钮初始状态
			resetAssetMenu();//复位物件编辑二级菜单
			menuAsset.btnCell.buttonMode=menuAsset.btnGrop.buttonMode=menuAsset.btnArea.buttonMode=true;
			
			//设置物件编辑为激活视图状态，地图编辑、地图测试操作按钮设默认为未激活视图状态
			btnComponentEdit.gotoAndStop(2);
			btnMapEdit.gotoAndStop(1);
			btnMapTest.gotoAndStop(1);
			
			_menuBg=new Sprite();
			_menuBg.y=61;
			addChildAt(_menuBg, 0);
		}
		
		/**
		 * 设置事件
		 */		
		private function setEvent():void
		{
			//设置操作按钮相关事件
			btnNewMap.addEventListener(MouseEvent.CLICK, __onButtonClick);
			btnOpenMap.addEventListener(MouseEvent.CLICK, __onButtonClick);
			btnSaveMap.addEventListener(MouseEvent.CLICK, __onButtonClick);
			btnComponentEdit.addEventListener(MouseEvent.CLICK, __onButtonClick);
			btnMapEdit.addEventListener(MouseEvent.CLICK, __onButtonClick);
			btnMapTest.addEventListener(MouseEvent.CLICK, __onButtonClick);
			btnMinimize.addEventListener(MouseEvent.CLICK, __onButtonClick);
			btnMaximize.addEventListener(MouseEvent.CLICK, __onButtonClick);
			btnClose.addEventListener(MouseEvent.CLICK, __onButtonClick);
			
			bgBar.addEventListener(MouseEvent.MOUSE_DOWN, __onMouseDown);
			bgBar.addEventListener(MouseEvent.DOUBLE_CLICK, __MouseDoubleClick);
			
			menuAsset.btnCell.addEventListener(MouseEvent.CLICK, __onMenuButtonClick);
			menuAsset.btnGrop.addEventListener(MouseEvent.CLICK, __onMenuButtonClick);
			menuAsset.btnArea.addEventListener(MouseEvent.CLICK, __onMenuButtonClick);
			menuAsset.btnCell.addEventListener(MouseEvent.MOUSE_OVER, __onMenuButtonMouseOver);
			menuAsset.btnGrop.addEventListener(MouseEvent.MOUSE_OVER, __onMenuButtonMouseOver);
			menuAsset.btnArea.addEventListener(MouseEvent.MOUSE_OVER, __onMenuButtonMouseOver);
			menuAsset.btnCell.addEventListener(MouseEvent.MOUSE_OUT, __onMenuButtonMouseOut);
			menuAsset.btnGrop.addEventListener(MouseEvent.MOUSE_OUT, __onMenuButtonMouseOut);
			menuAsset.btnArea.addEventListener(MouseEvent.MOUSE_OUT, __onMenuButtonMouseOut);
			menuAsset.btnCreateAsset.addEventListener(MouseEvent.CLICK, __onMenuButtonClick);
			
			addEventListener(Event.ADDED_TO_STAGE, __onAddedToStage);//当前视图增加到舞台后事件监听
		}
		
		/**
		 * 二级菜单点击事件 
		 * @param evt
		 * 
		 */		
		private function __onMenuButtonClick(evt:MouseEvent):void
		{
			//根据发出事件的按钮对象来进行事件处理
			switch(evt.currentTarget)
			{
				case menuAsset.btnCell://物件编辑-单项编辑
					_menuAssetType=1;
					_controller.setMenuAssetType(1);
					break;
				case menuAsset.btnGrop://物件编辑-组合编辑
					_menuAssetType=2;
					break;
				case menuAsset.btnArea://物件编辑-区域编辑
					_menuAssetType=3;
					break;
				case menuAsset.btnCreateAsset://创建单项物件
					break;
			}
			resetAssetMenu();
		}
		
		/**
		 * 二级菜单鼠标经过事件 
		 * @param evt
		 * 
		 */		
		private function __onMenuButtonMouseOver(evt:MouseEvent):void
		{
			evt.currentTarget.gotoAndStop(2);
		}
		
		/**
		 * 二级菜单鼠标离开事件 
		 * @param evt
		 * 
		 */		
		private function __onMenuButtonMouseOut(evt:MouseEvent):void
		{
			resetAssetMenu();
		}
		
		/**
		 * 在顶部栏按下 
		 * @param evt
		 * 
		 */		
		private function __onMouseDown(evt:MouseEvent):void
		{
			stage.nativeWindow.startMove();//移动窗口 
		}
		
		/**
		 * 双击顶部栏 
		 * @param evt
		 * 
		 */		
		private function __MouseDoubleClick(evt:MouseEvent):void
		{
			//最大化或还原窗口
			if(stage.nativeWindow.displayState==NativeWindowDisplayState.MAXIMIZED)
			{
				stage.nativeWindow.restore();
			}
			else
			{
				stage.nativeWindow.maximize();
			}
		}		
		
		/**
		 *设置背景UI
		 * 
		 */		
		private function setTopBarBgUI():void
		{
			if(!_bmTopBarRight) _bmTopBarRight=new Bitmap(new topBarBgRightUI());//创建顶部右侧UI
			_bmTopBarRight.x=stage.stageWidth-_bmTopBarRight.width;
			bgBar.addChild(_bmTopBarRight);
			
			if(_bmTopBarBg)
			{
				if(_bmTopBarBg.bitmapData) _bmTopBarBg.bitmapData.dispose();
				_bmTopBarBg=null;
			}
			if(!_topBarBgUI) _topBarBgUI=new topBarBgUI();
			bgBar.graphics.clear();
			bgBar.graphics.beginBitmapFill(_topBarBgUI);
			bgBar.graphics.drawRect(900, 0, stage.stageWidth-900-4, 61);
			bgBar.graphics.endFill();
			
			_menuBg.graphics.clear();
			_menuBg.graphics.beginBitmapFill(new menuBgUI());
			_menuBg.graphics.drawRect(0, 0, stage.stageWidth-3, 29);
			_menuBg.graphics.endFill();
		}
		
		/**
		 *当前视图增加到舞台后 
		 * @param evt
		 * 
		 */		
		private function __onAddedToStage(evt:Event):void
		{
			setTopBarBgUI();//设置背景UI
			stage.addEventListener(Event.RESIZE, __onResize);//窗口大小变化监听
		}
		
		/**
		 * 窗口发生变化时 
		 * @param evt
		 * 
		 */		
		private function __onResize(evt:Event):void
		{
			setTopBarBgUI();
			btnClose.x=stage.stageWidth-btnClose.width-5;
			btnMaximize.x=btnClose.x-btnMaximize.width;
			btnMinimize.x=btnMaximize.x-btnMinimize.width;
		}
		
		/**
		 * 操作按钮单击事件处理
		 * @param evt 鼠标事件
		 * 
		 */		
		private function __onButtonClick(evt:MouseEvent):void
		{
			//根据发出事件的按钮对象来进行事件处理
			switch(evt.currentTarget)
			{
				case btnNewMap://新建地图
					createMap();
					break;
				case btnOpenMap://打开地图
					
					break;
				case btnSaveMap://保存地图
					
					break;
				case btnComponentEdit://物件编辑
					_menuType=1;
					_controller.setMenuType(_menuType);
					break;
				case btnMapEdit://地图编辑
					_menuType=2;
					break;
				case btnMapTest://地图测试
					_menuType=3;
					break;
				case btnMinimize://最小化窗口
					stage.nativeWindow.minimize();
					break;
				case btnMaximize://最大化/还原窗口
					if(stage.nativeWindow.displayState==NativeWindowDisplayState.MAXIMIZED)
					{
						stage.nativeWindow.restore();
					}
					else
					{
						stage.nativeWindow.maximize();
					}
					break;
				case btnClose://关闭窗口
					stage.nativeWindow.close();
					break;
			}
		}
		
		/**
		 * 创建新地图 
		 * 
		 */		
		public function createMap():void
		{
			if(_createMapView)
			{
				if(_createMapView.parent) _createMapView.parent.removeChild(_createMapView);
				_createMapView.dispose();
			}
			_createMapView=null;
			
			_createMapView=new CreateMapView(_controller);
			DialogBoxManager.Instance.show(_createMapView, true, false);
		}
		
		/**
		 *复位操作按钮
		 */		
		private function resetButton():void
		{
			btnComponentEdit.gotoAndStop(1);
			btnMapEdit.gotoAndStop(1);
			btnMapTest.gotoAndStop(1);
		}
		
		/**
		 *复位物件编辑二级菜单 
		 * 
		 */		
		private function resetAssetMenu():void
		{
			menuAsset.btnCell.gotoAndStop(1);
			menuAsset.btnGrop.gotoAndStop(1);
			menuAsset.btnArea.gotoAndStop(1);
			
			//根据发出事件的按钮对象来进行事件处理
			switch(_menuAssetType)
			{
				case 1://单项编辑
					menuAsset.btnCell.gotoAndStop(2);
						break;
				case 2://组合编辑
					menuAsset.btnGrop.gotoAndStop(2);
					break;
				case 3://区域编辑
					menuAsset.btnArea.gotoAndStop(2);
					break;
			}
		}
	}
}